package com.dd.controller;

import com.dd.dto.LoginUserDTO;
import com.dd.entity.Employees;
import com.dd.entity.R;
import com.dd.enums.HttpEnum;
import com.dd.service.IEmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/employeeLogin")
public class LoginController {
    @Autowired
    IEmployeesService employeesService;

    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginUserDTO loginUserDTO) {
        Employees employees = employeesService.searchAllByLoginNameAndPassword(
                    loginUserDTO.getLoginName(), loginUserDTO.getPassword());
        if (employees != null) {
            return R.success(employees);
        } else {
            return R.fail(HttpEnum.UNAUTHORIZED.getCode(), "用户名或密码错误");
        }
    }
}
